package com.lotea.android.andreaditlater.ui.widgets;

import greendroid.widget.QuickActionWidget;
import greendroid.widget.QuickActionWidget.OnQuickActionClickListener;
import android.content.Context;
import android.database.Cursor;
import android.widget.Toast;

import com.lotea.android.andreaditlater.R;
import com.lotea.android.andreaditlater.data.db.DbAdapter;

public class ItemQuickAction extends greendroid.widget.QuickActionGrid
		implements OnQuickActionClickListener {

	private static final int POSITION_GOTO = 0;
	private static final int POSITION_TEXTONLY = 1;
	private static final int POSITION_MARK = 2;
	private static final int POSITION_TAGS = 3;
	private static final int POSITION_SHARE = 4;
	private static final int POSITION_DELETE = 5;

	private Context mContext;
	private int mId;
	private boolean mItemRead;

	public ItemQuickAction(Context context, int id) {
		super(context);

		mContext = context;
		mId = id;

		addQuickAction(new greendroid.widget.QuickAction(context,
				R.drawable.ic_goto, "Go to"));
		addQuickAction(new greendroid.widget.QuickAction(context,
				R.drawable.text_only, "Text only"));

		// Show "read" or "unread" depending on state value
		mItemRead = isItemRead(context, id);
		if (!mItemRead) {
			addQuickAction(new greendroid.widget.QuickAction(context,
					R.drawable.mark_read, "Mark as read"));
		} else {
			addQuickAction(new greendroid.widget.QuickAction(context,
					R.drawable.mark_unread, "Mark as unread"));
		}

		addQuickAction(new greendroid.widget.QuickAction(context,
				R.drawable.tag, "Manage tags"));
		addQuickAction(new greendroid.widget.QuickAction(context,
				R.drawable.ic_title_share_alt, "Share"));
		addQuickAction(new greendroid.widget.QuickAction(context,
				R.drawable.delete, "Delete"));
		
		setOnQuickActionClickListener(this);
	}

	private boolean isItemRead(Context context, int id) {
		// Getting state for pressed item
		StringBuilder builder = new StringBuilder();
		builder.append("SELECT ");
		builder.append(DbAdapter.Columns.Items.STATE);
		builder.append(" FROM ");
		builder.append(DbAdapter.Tables.ITEMS);
		builder.append(" WHERE ");
		builder.append(DbAdapter.Columns.ID);
		builder.append(" = ?");

		DbAdapter mDbAdapter = new DbAdapter(context);
		mDbAdapter.open();
		Cursor cursor = mDbAdapter.rawQuery(builder.toString(),
				new String[] { Integer.toString(id) });
		int state = cursor.getInt(cursor
				.getColumnIndexOrThrow(DbAdapter.Columns.Items.STATE));
		cursor.close();
		mDbAdapter.close();

		return state == 1;
	}

	@Override
	public void onQuickActionClicked(QuickActionWidget widget, int position) {
		switch (position) {
		case POSITION_GOTO:
			Toast.makeText(mContext, "Go to!", Toast.LENGTH_SHORT).show();
			break;

		case POSITION_TEXTONLY:
			Toast.makeText(mContext, "Text only!", Toast.LENGTH_SHORT).show();
			break;

		case POSITION_MARK:
			String text = "";
			if (mItemRead) {
				text = "Mark as unread!";
			} else {
				text = "Mark as read!";
			}
			Toast.makeText(mContext, text, Toast.LENGTH_SHORT).show();
			break;

		case POSITION_SHARE:
			Toast.makeText(mContext, "Share!", Toast.LENGTH_SHORT).show();
			break;

		case POSITION_TAGS:
			Toast.makeText(mContext, "Tags!", Toast.LENGTH_SHORT).show();
			break;

		case POSITION_DELETE:
			Toast.makeText(mContext, "Delete!", Toast.LENGTH_SHORT).show();
			break;
		}
		
		dismiss();
	}
}
